package work;

/**
 * Created with IntelliJ IDEA
 * Description:
 * 有20个线程，需要同时启动。
 * 每个线程按0-19的序号打印，如第一个线程需要打印0
 * 请设计代码，在main主线程中，等待所有子线程执行完后，再打印 ok
 * User：chenshu
 * Date：2023-11-05
 * Time：15:54
 */
public class Demo1 {
    public static void main(String[] args) {
        Thread[] threads = new Thread[20];
        threads[0] = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("0");
            }
        });
        for (int i = 1; i < 20; i++) {
            int n = i;
            threads[n] = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        threads[n-1].join();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(n);
                }
            });
        }
        for(Thread t : threads) {
            t.start();
        }
        for(Thread t : threads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("OK");
    }
}
